package com.ruoyi.common.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;

import javax.annotation.Resource;
import java.security.PrivateKey;

/**
 * @ClassName WxPayConfig
 * @Description TODO
 * @Author Mr、Liu
 * @Date 2025/6/26 0:14
 * @Version 1.0
 */
@Configuration
public class WxPayConfig {
    @Resource
    private WxPayProperties properties;

    @Bean
    public Config openWechatConfig(WxPayProperties prop) throws Exception
    {
        ClassPathResource resource = new ClassPathResource(properties.getKeyPath());
        PrivateKey privateKey = PemUtil.loadPrivateKey(resource.getInputStream());
        properties.setPrivateKey(privateKey);
        return new RSAAutoCertificateConfig.Builder()
                .merchantId(properties.getMchId())
                .privateKey(privateKey)
                .merchantSerialNumber(properties.getMchSerialNumber())
                .apiV3Key(properties.getApiV3Key())
                .build();
    }
}
